﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;

namespace WebCore.Bases
{
    public class BaseController : Controller
    {
        protected override void OnException(ExceptionContext filterContext)
        {
            if (filterContext == null)
                return;
            // since we're handling this, log to elmah
            if (filterContext.Exception != null)
            {
                var ex = filterContext.Exception;
                LogExeption(ex as Exception);
            }
        }


        private void LogExeption(Exception e)
        {
            using (StreamWriter sw = System.IO.File.AppendText(System.Web.HttpContext.Current.Server.MapPath("~/logs/error/" + String.Format("{0:yyyy-MM}", DateTime.Today) + ".txt")))
            {
                sw.WriteLine("--------- " + Convert.ToString(DateTime.Now) + " --------- --------- --------- --------- ---------");
                if (e != null)
                {
                    sw.WriteLine("Message: " + e.Message);
                    sw.WriteLine("Stack Trace: ");
                    sw.WriteLine(e.StackTrace);
                    sw.WriteLine("");
                }
                sw.Close();
            }
        }
    }
}